#include "sys.h"
#include "mainInclude.h"
#include "misc.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_can.h"

void NVIC_Group_Init(void)	//系统中断分组
{
	NVIC_PriorityGroupConfig(BOARD_NVIC_GROUP);//配置系统中断
}

/********************************************************************
// 功能:  串口初始化
// 修改历史:
// 固定11bit的数据
0:无校验 bit0 起始位 b1-b8 数据位  b9-b10 结束位
1/2:奇偶校验 bit0 起始位 b1-b8 数据位  b9 校验  b10 结束位
***********************************************************************/
uint8_t gPCCOMM_RxValueTab[512]={0};
uint8_t gPCCOMM_TxValueTab[512]={0};
void Debug_Serial_Init(u32 baud)
{
    USART_InitTypeDef USART_InitStructure;
	GPIO_InitTypeDef  GPIO_InitStructure;
	DMA_InitTypeDef   DMA_InitStructure;

	GPIO_StructInit(&GPIO_InitStructure);
	DMA_StructInit(&DMA_InitStructure);
	USART_StructInit(&USART_InitStructure);
 
    RCC_AHB1PeriphClockCmd(PC_GPIO_RCC_Periph, ENABLE);
    RCC_APB2PeriphClockCmd(PC_USART_RCC_Periph, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); // 开启DMA2时钟  

	GPIO_PinAFConfig(PC_GPIO_TX,  PC_GPIO_Source_TX,   PC_GPIO_AF);
	GPIO_PinAFConfig(PC_GPIO_RX,  PC_GPIO_Source_RX,   PC_GPIO_AF);

	GPIO_InitStructure.GPIO_Pin = PC_GPIO_PIN_TX; //
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	//速度50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
	GPIO_Init(PC_GPIO_TX,&GPIO_InitStructure); 

	GPIO_InitStructure.GPIO_Pin = PC_GPIO_PIN_RX; //
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	//速度50MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
	GPIO_Init(PC_GPIO_RX,&GPIO_InitStructure); 

	//RXDMA
	DMA_Cmd(DMA2_Stream2, DISABLE);  //关DMA2通道
	DMA_DeInit(DMA2_Stream2);
	DMA_InitStructure.DMA_Channel            = DMA_Channel_5;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (PC_DMA_BaseAddr+0x04);
	DMA_InitStructure.DMA_Memory0BaseAddr    = (uint32_t)gPCCOMM_RxValueTab;
	DMA_InitStructure.DMA_DIR                = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize         = 512;
	DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode               = DMA_Mode_Circular;
	DMA_InitStructure.DMA_Priority           = DMA_Priority_High;
	DMA_Init(DMA2_Stream2, &DMA_InitStructure);
	DMA_ClearFlag(DMA2_Stream2,DMA_FLAG_TCIF2);

	//TXDMA
	DMA_Cmd(DMA2_Stream6, DISABLE);  //关DMA2通道
	DMA_DeInit(DMA2_Stream6);
	DMA_InitStructure.DMA_Channel            = DMA_Channel_5;
	DMA_InitStructure.DMA_PeripheralBaseAddr = (PC_DMA_BaseAddr+0x04);
	DMA_InitStructure.DMA_Memory0BaseAddr    = (uint32_t)gPCCOMM_TxValueTab;
	DMA_InitStructure.DMA_DIR                = DMA_DIR_MemoryToPeripheral;
	DMA_InitStructure.DMA_BufferSize         = 512;
	DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;
	DMA_InitStructure.DMA_Mode               = DMA_Mode_Circular;
	DMA_InitStructure.DMA_Priority           = DMA_Priority_High;
	DMA_Init(DMA2_Stream6, &DMA_InitStructure);
	DMA_ClearFlag(DMA2_Stream6,DMA_FLAG_TCIF6);
 
	USART_DeInit(PC_USART);
	
	USART_InitStructure.USART_BaudRate	= baud;
    USART_InitStructure.USART_StopBits	= USART_StopBits_2;
	USART_InitStructure.USART_Parity	= USART_Parity_No;
    USART_InitStructure.USART_WordLength= USART_WordLength_8b;	
	USART_InitStructure.USART_HardwareFlowControl	= USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode					= USART_Mode_Rx | USART_Mode_Tx;
	USART_Init(PC_USART, &USART_InitStructure);	
	
	USART_Cmd(PC_USART, ENABLE);

	USART_DMACmd(PC_USART, USART_DMAReq_Rx, ENABLE); // 开启串口DMA接收

	USART_DMACmd(PC_USART, USART_DMAReq_Tx, ENABLE); // 开启串口DMA发送

	DMA_ClearFlag(DMA2_Stream6,DMA_FLAG_TCIF6); //清除DMA2_Steam7传输完成标志

	DMA_Cmd(DMA2_Stream2, ENABLE); 

	DMA_Cmd(DMA2_Stream6, DISABLE); 
}

void IWDG_Configuration(void)
{
    //使能对寄存器IWDG_PR和IWDG_RLR的写操作
    IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
	
    //设置IWDG预分频值256:
    //4*200/40 = 20ms；
    IWDG_SetPrescaler(IWDG_Prescaler_4);

    //设置IWDG重装载值，要小于0xfff（因为寄存器是12位的）
    IWDG_SetReload(2000);  
    //按照IWDG重装载寄存器的值 重新载入IWDG计数值；
    IWDG_ReloadCounter();
    IWDG_Enable();    //使能IWDG

	DBGMCU_Config(DBGMCU_IWDG_STOP, ENABLE);
}

void IniLedIo(void)
{
 	GPIO_InitTypeDef GPIO_InitStruct;  //初始化结构体

    GPIO_StructInit(&GPIO_InitStruct);

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;          
 	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOE, &GPIO_InitStruct); 

	GPIO_StructInit(&GPIO_InitStruct);
}

//TIM6 定时器
void OSTimerInit(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  uint16 PrescalerValue = 0;  
	
	RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM6,ENABLE );				//基础设置，时基和比较输出设置，由于这里只需定时，所以不用OC比较输出	
	PrescalerValue = 90 - 1;//(uint16_t) (72000 / 500000) - 1;	//计算分频系数
	
	TIM_DeInit(TIM6);
	TIM_TimeBaseStructure.TIM_Period=100;				   //a period is 1000 times   //改低为200HZ,5MS
	TIM_TimeBaseStructure.TIM_Prescaler=PrescalerValue;    //division number	is 72		//72000000/72
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;				//set clock division //or TIM_CKD_DIV2 or TIM_CKD_DIV4
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;			//count up
	
	TIM_TimeBaseInit(TIM6,&TIM_TimeBaseStructure);
	TIM_ClearFlag(TIM6,TIM_FLAG_Update);							//clear the TIM2 overflow interrupt flag
	TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE);						//TIM2 overflow interrupt enable
	TIM_Cmd(TIM6,ENABLE);																//enable TIM2
																//enable TIM2
}

/***************************************************************
*	void NVIC_Configuration(void)																 *
****************************************************************/
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
	
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);              //???????1:1
    
	//TIM6中断
	NVIC_InitStructure.NVIC_IRQChannel=TIM6_DAC_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

void NVIC_Configuration_Rset(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
	
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);             //中断优先级分组2:2
    
	//0.1ms中断
	NVIC_InitStructure.NVIC_IRQChannel=TIM6_DAC_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
	NVIC_Init(&NVIC_InitStructure);
}




